package com.huitone.smspfm.oldclient.exception;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.List;

import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.oldclient.logger.ExtLogger;

/**   
* @Title SimpleUncaughtExceptionHandler.java 
* @Package com.huitone.smspfm.interfacz.exception 
* @Description 全局未捕捉异常处理类
* @author ybliang   
* @date 2017年10月21日 上午12:54:42 
* @version V1.0   
*/
public class SimpleUncaughtExceptionHandler implements UncaughtExceptionHandler {
	
	private static final ExtLogger logger = ExtLogger.create(SimpleUncaughtExceptionHandler.class);

	@Override
	public void uncaughtException(Thread t, Throwable e) {
		
		List<Throwable> list = new ArrayList<Throwable>();
		
		do {
			
			if (e != null) {
				list.add(e);
				e = e.getCause();
			}
			
		} while ( e != null);
		
		handler(list);
	}
	
	public void handler(List<Throwable> list) {
		
		boolean printDetails = true;
		
		if (containsClassName(list, "JedisConnectionException", "Redis连接错误, 请检查Redis内存数据库是否可用, Redis.properties是否配置正确")) {
		} else {
		}
		
		if (printDetails) {
			logger.error(list.get(0), list.get(0));
		}
	}
	
	public boolean containsClassName(List<Throwable> list, String className, String message) {
		
		for (Throwable tb:list) {
			if (StringUtils.equals(tb.getClass().getSimpleName(), className)) {
				logger.error(message + (StringUtils.isEmpty(message) ? "":": ") + tb.getMessage());
				return true;
			}
		}
		
		return false;
	}
}
